Parameters m.Mascara, m.tiporeg

Private Escolhido, ltalk, cmat, cser, ctax, areanti

if set('Talk') = 'ON'
   set talk off
   ltalk = .t.
  else
   ltalk = .f.
endif

m.areanti = select()

if empty(m.tiporeg)
   m.tiporeg = 'SMT'
endif

m.Escolhido = ''

select codigo, descricao from servicos ;
 where substr(codigo,3,5)='00000' ;
   and Codigo Like m.Mascara ;
   and MatSer $    m.TipoReg ;
 order by codigo ;
  into cursor cEsp1

push key clear

DEFINE POPUP pEspecie from 2, 13 to srows()-2, scols()-7 ;
       color scheme 9 shadow ;
       Margin Mark '' ;
       Title ' ESPECIALIDADES ' ;
       PROMPT FIELD padr(cEsp1.Descricao,45) +' ' + ;
                    transform( cEsp1.Codigo, '@R !!.99.999-9' )

ON SELECTION POPUP pEspecie DO VerSub

ACTIVATE POPUP pEspecie

RELEASE POPUP pEspecie, pEsp2, pEsp3 extended

pop key

use

if used('cEsp1')
   select cEsp1
   use
endif

if used('cEsp2')
   select cEsp2
   use
endif

if used('cEsp3')
   select cEsp3
   use
endif

Select (m.Areanti)

if ltalk
   set talk on
endif

return m.Escolhido

***********************************************************************

Procedure VerSub

if eof()
   = tone(1300,1)
   return
endif

m.SubCod = left( cEsp1.Codigo , 2)
m.codesp = cEsp1.Codigo
m.desc1  = ' ' + alltrim( cEsp1.Descricao ) + ' '

select codigo, descricao from servicos ;
 where Codigo Like m.Mascara    ;
   and substr(codigo,5,3)='000' ;
   and left(codigo,2) = m.SubCod ;
   and Codigo != m.codesp ;
   and MatSer $    m.TipoReg ;
 order by codigo ;
  into cursor cEsp2
  
Push Key Clear

DEFINE POPUP pEsp2 from 3, 15 to srows()-2, scols()-5 ;
       color scheme 9 shadow ;
       Title (m.Desc1) ;
       Margin Mark '' ;
       PROMPT FIELD padr(cEsp2.Descricao,45) +' ' + ;
                    transform( cEsp2.Codigo, '@R !!.99.999-9' )

ON SELECTION POPUP pEsp2 DO VerFinal

ACTIVATE POPUP pEsp2

RELEASE POPUP pEsp2 extended

pop key

use

Select cEsp1

*********************************************************************

Procedure VerFinal

if eof()
   = tone(1300,1)
   return
endif

m.SubSubCod = left( cEsp2.Codigo , 4)
m.codsubesp = cEsp2.Codigo
m.desc2     = ' ' + alltrim( cEsp2.Descricao) + ' '

select codigo, descricao from servicos ;
 where Codigo Like m.Mascara ;
   and left(codigo,4) = m.SubSubCod ;
   and Detalhe ;
   and MatSer $    m.TipoReg ;
 order by codigo ;
  into cursor cEsp3
  
Push Key Clear

DEFINE POPUP pEsp3 from 4, 17 to srows()-2, scols()-3 ;
       color scheme 9 shadow ;
       Title (m.Desc2) ;
       Margin Mark '' ;
       PROMPT FIELD padr(cEsp3.Descricao,45) +' ' + ;
                    transform( cEsp3.Codigo, '@R !!.99.999-9' )

ON SELECTION POPUP pEsp3 DO Escolher

ACTIVATE POPUP pEsp3

RELEASE POPUP pEsp3 extended

pop key
use

Select cEsp2

Procedure Escolher

          if eof()
             return
          endif
          
          m.escolhido = cEsp3.Codigo
          
          deactivate popup all
